/******************************************************************************
* File description: speechModuleThread.cpp
* Function: 语音助手线程
* Current Version: 1.0.0
* Author: 郑佰旺
* Date: 2025-07-1
*  -----
*  Copyright by 博坤生物 .
 *****************************************************************************/

#include "SpeechModuleThread.h"

speechModuleThread::speechModuleThread(QObject *parent): QObject(parent)
{
    this->moveToThread(&thread);
    serialPort = new QSerialPort(this);
    thread.start();
    serialinfo();
}

/**
 * @brief speechModuleThread::serialinfo 读取串口信息
 */
void speechModuleThread::serialinfo()
{
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="Select * From SpeechMode";
    query.exec(sql);
    QString serialNamePort;
    bool isUse=false;
    while (query.next())
    {
        serialNamePort=query.value("SerialPort").toString();
        isUse=query.value("IsUse").toString()=="1";
    }
    if(isUse){
        if(serialPort->isOpen()){
            serialPort->close();
        }else{
            QSerialPort::BaudRate baudRate;
            baudRate = QSerialPort::Baud115200;
            serialPort->setPortName(serialNamePort);
            serialPort->setBaudRate(baudRate);
            if(serialPort->open(QIODevice::ReadWrite)){
                QMLSignal::I().emitVoiceAssistant(0);     // 发信号通知前端
            }else{
                QMLSignal::I().emitVoiceAssistant(1);     // 发信号通知前端
            }
            connect(serialPort,SIGNAL(readyRead()),this,SLOT(serialPortRead_Slot()));// 串口读取
        }
    }
    else{
        QMLSignal::I().emitVoiceAssistant(1);     // 发信号通知前端
    }
}

/**
 * @brief speechModuleThread::serialPortRead_Slot 处理接收到的消息数据
 */
void speechModuleThread::serialPortRead_Slot()
{
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="Select * From SpeechMode";
    query.exec(sql);
    QString serialNamePort;
    bool isUse;
    while (query.next())
    {
        serialNamePort=query.value("SerialPort").toString();
        isUse=query.value("IsUse").toString()=="1";
    }
    if(isUse){
        udpClient=UdpClient::getInstance();
        QByteArray buffer = serialPort->readAll();
        QString ss;
        for(int c :buffer)
        {
            if(c>=0)
            {
                ss += QString(" %1")
                        .arg(c, 2, 16, QChar('0'));
            }
            else
            {
                ss += QString(" %1")
                        .arg(c+256, 2, 16, QChar('0'));
            }
        }
        QString Data=ss.remove(QRegExp("\\s"));
        if(Data=="0001")//启动流程实验
        {
            if(StaticProperties::currentExpType!=0)//当前选择的实验流程不是全流程
            {
                return;
            }
            else if(StaticProperties::currentExpConsumables.isNull())//没有选择耗材类型
            {
                return;
            }
            else if(StaticProperties::releasseTargetStatus.value("0")!="有")
            {
                return;
            }
            else if(StaticProperties::purifyingStatus.value("2")!="有")//纯化是否存在耗材
            {
                return;
            }
            else if(StaticProperties::expStatus!=0)
            {
                return;
            }
            else if(StaticProperties::runningList.length()>0)
            {
                return;
            }
            else if(ExpCommonMethod::Common().checkConsumablesSyring(CommonHelper::Common().getItemName("3",StaticProperties::currentExpConsumables))!="")
            {
                return;
            }
            else{
                StaticProperties::currentExpModule=1;
                StaticProperties::totalExpTime=0;
                QMLSignal::I().emitGetExperimentInfo();
                ExpCommonMethod::Common().analysisTotalProcessExp(StaticProperties::currentExpName);
//                udpClient->ClientSendingData(CommonHelper::Common().GetSendData( CommCommand::I().startExperiment()));
                ExpCommonMethod::Common().saveExpInfo();
            }
        }
        else if(Data.toUpper()=="000C")//启动放靶实验
        {
            if(StaticProperties::currentExpType!=1)//当前选择的实验流程不是全流程
            {
                return;
            }
            else if(StaticProperties::releasseTargetStatus.value("0")!="有")
            {
                return;
            }
            else if(StaticProperties::expStatus!=0)
            {
                return;
            }
            else if(StaticProperties::runningList.contains("10"))
            {
                return;
            }
            else if(StaticProperties::currentExpName==""){
                return;
            }

            StaticProperties::currentExpModule=1;
            StaticProperties::totalExpTime=0;
            QMLSignal::I().emitGetExperimentInfo();

            ExpCommonMethod::Common().analysisReleasseTargetExp(StaticProperties::currentExpName);
//            udpClient->ClientSendingData(CommonHelper::Common().GetSendData( CommCommand::I().startExperiment()));
            ExpCommonMethod::Common().saveExpInfo();
        }
        else if(Data.toUpper()=="000D")//启动打靶实验
        {
            if(StaticProperties::currentExpType!=2)//当前选择的实验流程不是全流程
            {
                return;
            }
            else if(StaticProperties::practiceTargetStatus.value("0")!="有")
            {
                return;
            }
            else if(StaticProperties::expStatus!=0)
            {
                return;
            }
            else if(StaticProperties::runningList.contains("11"))
            {
                return;
            }
            else if(StaticProperties::currentExpName==""){
                return;
            }
            StaticProperties::currentExpModule=2;
            StaticProperties::totalExpTime=0;
            QMLSignal::I().emitGetExperimentInfo();

            ExpCommonMethod::Common().analysisPracticeTargetExp(StaticProperties::currentExpName);
//            udpClient->ClientSendingData(CommonHelper::Common().GetSendData( CommCommand::I().startExperiment()));
            ExpCommonMethod::Common().saveExpInfo();
        }
        else if(Data.toUpper()=="000E")//启动转靶实验
        {
            if(StaticProperties::currentExpType!=3)//当前选择的实验流程不是全流程
            {
                return;
            }
            else if(StaticProperties::releasseTargetStatus.value("0")!="有")
            {
                return;
            }
            else if(StaticProperties::expStatus!=0)
            {
                return;
            }
            else if(StaticProperties::runningList.contains("10"))
            {
                return;
            }
            else if(StaticProperties::currentExpName==""){
                return;
            }
            //联动未校验
            StaticProperties::currentExpModule=3;
            StaticProperties::totalExpTime=0;
            QMLSignal::I().emitGetExperimentInfo();

            ExpCommonMethod::Common().analysisShiftingTargetExp(StaticProperties::currentExpName);
//            udpClient->ClientSendingData(CommonHelper::Common().GetSendData( CommCommand::I().startExperiment()));
            ExpCommonMethod::Common().saveExpInfo();
        }
        else if(Data.toUpper()=="000F")//启动纯化实验
        {
            if(StaticProperties::currentExpType!=4)//当前选择的实验流程不是全流程
            {
                return;
            }
            else if(StaticProperties::currentExpConsumables.isNull())//没有选择耗材类型
            {
                return;
            }
            else if(StaticProperties::purifyingStatus.value("2")!="有")//纯化是否存在耗材
            {
                return;
            }
            else if(StaticProperties::expStatus!=0)
            {
                return;
            }
            else if(!StaticProperties::runningList.contains("10") &&!StaticProperties::runningList.contains("11")&& StaticProperties::runningList.length()>0)
            {
                return;
            }
            else if(ExpCommonMethod::Common().checkConsumablesSyring(CommonHelper::Common().getItemName("3",StaticProperties::currentExpConsumables))!="")
            {
                return;
            }
            else if(StaticProperties::currentExpName==""){
                return;
            }
            else{
                StaticProperties::currentExpModule=4;
                StaticProperties::totalExpTime=0;
                QMLSignal::I().emitGetExperimentInfo();

                ExpCommonMethod::Common().analysisPurifyingExp(StaticProperties::currentExpName);
//                udpClient->ClientSendingData(CommonHelper::Common().GetSendData( CommCommand::I().startExperiment()));
                ExpCommonMethod::Common().saveExpInfo();
            }

        }
        else if(Data=="0002")//停止实验
        {
            if(StaticProperties::expStatus==0)
            {
                //                QMediaPlayer *player = new QMediaPlayer;
                //                player->setMedia(QUrl("qrc:/mp3/test.mp3"));
                //                player->play();
                return;
            }
            else{
                udpClient->ClientSendingData(CommonHelper::Common().GetSendData( CommCommand::I().stopExperiment()));
            }
        }
        else if(Data=="0003")//暂停实验
        {
            if(StaticProperties::expStatus==0)
            {
                //                QMediaPlayer *player = new QMediaPlayer;
                //                player->setMedia(QUrl("qrc:/mp3/test.mp3"));
                //                player->play();
                return;
            }
            else{
                StaticProperties::expStatus=2;
                if(!StaticProperties::isWait){
                    udpClient->ClientSendingData(CommonHelper::Common().GetSendData( CommCommand::I().pauseExperiment()));
                }
                else{
                    QMLSignal::I().experimentStatus(3,0,"");
                }
            }
        }
        else if(Data=="0004")//继续实验
        {
            if(StaticProperties::expStatus==0)
            {
                //                QMediaPlayer *player = new QMediaPlayer;
                //                player->setMedia(QUrl("qrc:/mp3/test.mp3"));
                //                player->play();
                return;
            }
            else if(StaticProperties::expStatus!=2){
                return;
            }
            else{
                if(StaticProperties::expStatus==2){
                    StaticProperties::expStatus=1;
                    if(!StaticProperties::isWait){
                        udpClient->ClientSendingData(CommonHelper::Common().GetSendData( CommCommand::I().continueExperiment()));
                    }
                    else{
                        QMLSignal::I().experimentStatus(4,0,"");
                    }
                }else{
                    QMLSignal::I().experimentStatus(4,0,"");
                    ExperimentThread::I().run();
                }
            }
        }
        else if(Data=="0006")//全部复位
        {
            if(StaticProperties::expStatus!=0){
                return;
            }

            else if(StaticProperties::currentExpConsumables.isNull())//没有选择耗材类型
            {
                return;
            }
            else if(StaticProperties::runningList.length()>0)
            {
                return;
            }
            QString info = CommCommand::I().reset("1","0",StaticProperties::currentExpConsumables);
            if(!info.isNull()){
                udpClient->ClientSendingData(CommonHelper::Common().GetSendData(info));
            }
        }
        else if(Data=="0010")//复位放靶模块
        {
            if(StaticProperties::expStatus!=0){

            }
            else if(StaticProperties::runningList.contains("10"))
            {
                return;
            }
            QString info = CommCommand::I().reset("1","10","");
            if(!info.isNull()){
                udpClient->ClientSendingData(CommonHelper::Common().GetSendData(info));
            }
        }
        else if(Data=="0011")//复位打靶模块
        {
            if(StaticProperties::expStatus!=0){

            }
            else if(StaticProperties::runningList.contains("11"))
            {
                return;
            }
            QString info = CommCommand::I().reset("1","11","");
            if(!info.isNull()){
                udpClient->ClientSendingData(CommonHelper::Common().GetSendData(info));
            }
        }
        else if(Data=="0012")//复位纯化模块
        {
            if(StaticProperties::expStatus!=0){
                return;
            }
            else if(StaticProperties::currentExpConsumables.isNull())//没有选择耗材类型
            {
                return;
            }
            else if(!StaticProperties::runningList.contains("10") &&!StaticProperties::runningList.contains("11")&& StaticProperties::runningList.length()>0)
            {
                return;
            }
            else{
                QString info = CommCommand::I().reset("1","9",StaticProperties::currentExpConsumables);
                if(!info.isNull()){
                    udpClient->ClientSendingData(CommonHelper::Common().GetSendData(info));
                }
            }
        }
    }
}

/**
 * @brief speechModuleThread::serialclo 关闭通信
 */
void speechModuleThread::serialclo(){
    serialPort->close();
}
